home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / BUTTONS / BORBTN32 / BORBTNS.PAS < prev    next >
Pascal/Delphi Source File  |  1996-03-24  |  15KB  |  571 lines

  1. {-------------------------------------------------------------------}
  2. { BORBTNS - BWCC Style CheckBoxes & Radio Buttons for Delphi v 1.02 }
  3. {-------------------------------------------------------------------}
  4. { v. 1.00 April, 8 1995                                             }
  5. { v. 1.01  July, 6 1995  Controls refreshed when caption changes    }
  6. { v. 1.02 March,24 1996  Delphi 32 compatible version               }
  7. {-------------------------------------------------------------------}
  8. { Copyright Enrico Lodolo                                           }
  9. { via F.Bolognese 27/3 - 440129 Bologna - Italy                     }
  10. { CIS 100275,1255 - Internet ldlc18k1@bo.nettuno.it                 }
  11. {-------------------------------------------------------------------}
  12.  
  13. unit BorBtns;
  14.  
  15. interface
  16.  
  17. uses
  18.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  19.   Forms, Dialogs, StdCtrls, Menus;
  20.  
  21. type
  22.   TBorCheck = class(TCustomControl)
  23.   private
  24.     FDown:Boolean;
  25.     FState:TCheckBoxState;
  26.     FFocused:Boolean;
  27.     FCheckColor:TColor;
  28.   protected
  29.     constructor Create(AOwner: TComponent); override;
  30.     procedure Paint; override;
  31.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  32.       X, Y: Integer); override;
  33.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  34.       X, Y: Integer); override;
  35.     procedure MouseMove(Shift: TShiftState;X, Y: Integer);
  36.       override;
  37.     procedure KeyDown(var Key:Word;Shift:TShiftSTate); override;
  38.     procedure KeyUp(var Key:Word;Shift:TShiftSTate); override;
  39.     procedure SetDown(Value:Boolean);
  40.     procedure SetState(Value:TCheckBoxState);
  41.     procedure SetChecked(Value:Boolean);
  42.     function  GetChecked:Boolean;
  43.     procedure SetCheckColor(Value:TColor);
  44.     function  GetCaption: TCaption;
  45.     procedure SetCaption(const Value:TCaption);
  46.     procedure DoEnter; override;
  47.     procedure DoExit; override;
  48.   public
  49.   published
  50.     property Caption:TCaption read GetCaption write SetCaption;
  51.     property CheckColor:TColor read FCheckColor write SetCheckColor
  52.              default clBlack;
  53.     property Checked:Boolean read GetChecked write SetChecked
  54.              default False;
  55.     property Down:Boolean read FDown write SetDown default False;
  56.     property DragCursor;
  57.     property DragMode;
  58.     property Font;
  59.     property ParentFont;
  60.     property PopupMenu;
  61.     property ShowHint;
  62.     property State:TCheckBoxState read FState write SetState
  63.              default cbUnchecked;
  64.     property TabOrder;
  65.     property TabStop;
  66.     property OnClick;
  67.     property OnDragDrop;
  68.     property OnDragOver;
  69.     property OnEndDrag;
  70.     property OnKeyDown;
  71.     property OnKeyPress;
  72.     property OnKeyUp;
  73.     property OnMouseDown;
  74.     property OnMouseMove;
  75.     property OnMouseUp;
  76.   end;
  77.  
  78. type
  79.   TBorRadio = class(TCustomControl)
  80.   private
  81.     FDown:Boolean;
  82.     FChecked:Boolean;
  83.     FFocused:Boolean;
  84.     FCheckColor:TColor;
  85.     FGroupIndex:Byte;
  86.     procedure TurnSiblingsOff;
  87.   protected
  88.     constructor Create(AOwner: TComponent); override;
  89.     procedure Paint; override;
  90.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  91.       X, Y: Integer); override;
  92.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  93.       X, Y: Integer); override;
  94.     procedure MouseMove(Shift: TShiftState;X, Y: Integer);
  95.       override;
  96.     procedure KeyDown(var Key:Word;Shift:TShiftSTate); override;
  97.     procedure KeyUp(var Key:Word;Shift:TShiftSTate); override;
  98.     function  GetCaption: TCaption;
  99.     procedure SetCaption(const Value:TCaption);
  100.     procedure SetDown(Value:Boolean);
  101.     procedure SetChecked(Value:Boolean);
  102.     procedure SetCheckColor(Value:TColor);
  103.     procedure DoEnter; override;
  104.     procedure DoExit; override;
  105.   public
  106.   published
  107.     property Caption:TCaption read GetCaption write SetCaption;
  108.     property CheckColor:TColor read FCheckColor write SetCheckColor
  109.              default clBlack;
  110.     property Checked:Boolean read FChecked write SetChecked
  111.              default False;
  112.     property Down:Boolean read FDown write SetDown default False;
  113.     property DragCursor;
  114.     property DragMode;
  115.     property Font;
  116.     property GroupIndex:Byte read FGroupIndex write FGroupIndex
  117.       default 0;
  118.     property ParentFont;
  119.     property PopupMenu;
  120.     property ShowHint;
  121.     property TabOrder;
  122.     property TabStop;
  123.     property OnClick;
  124.     property OnDragDrop;
  125.     property OnDragOver;
  126.     property OnEndDrag;
  127.     property OnKeyDown;
  128.     property OnKeyPress;
  129.     property OnKeyUp;
  130.     property OnMouseDown;
  131.     property OnMouseMove;
  132.     property OnMouseUp;
  133.   end;
  134.  
  135. procedure Register;
  136.  
  137. implementation
  138.  
  139. {-------------------------------------------------------------------}
  140. {                          Borland Style CheckBox                   }
  141. {-------------------------------------------------------------------}
  142.  
  143. constructor TBorCheck.Create(AOwner: TComponent);
  144.  
  145. begin
  146.   inherited Create(AOwner);
  147.   Width := 98;
  148.   Height := 20;
  149.   ParentColor:=False;
  150.   Color:=clBtnFace;
  151. end;
  152.  
  153. const BW=12;
  154.  
  155. procedure TBorCheck.Paint;
  156.  
  157. var BL,BT,BR,BB:Integer;
  158.     TX,TY,TW,TH:Integer;
  159.     Rect:TRect;
  160.  
  161. begin
  162.      Canvas.Font:=Font;
  163.      with Canvas do
  164.        begin
  165.          BT:=(Height div 2)-(BW div 2);
  166.          BB:=BT+BW;
  167.          BL:=1;
  168.          BR:=BW+1;
  169.          Brush.Color:=clBtnFace;
  170.          if not FDown then
  171.            begin
  172.              Pen.Color:=clBtnFace;
  173.              Rectangle(BL,BT,BR,BB);
  174.              Pen.Color:=clBtnHighLight;
  175.              MoveTo(BL,BB);
  176.              LineTo(BL,BT);
  177.              LineTo(BR,BT);
  178.              Pen.Color:=clBtnShadow;
  179.              LineTo(BR,BB);
  180.              LineTo(BL,BB);
  181.            end
  182.          else
  183.            begin
  184.              Pen.Color:=clBlack;
  185.              Pen.Width:=2;
  186.              Rectangle(BL+1,BT+1,BR+1,BB+1);
  187.              Pen.Width:=1;
  188.            end;
  189.          TX:=BR+5;
  190.          TY:=(Height div 2)+(Font.Height div 2)-1;
  191.          TW:=TextWidth(Caption);
  192.          TH:=TextHeight(Caption);
  193.          TextOut(TX,TY,Caption);
  194.          case State of
  195.            cbChecked:begin
  196.                        Pen.Color:=FCheckColor;
  197.                        Pen.Width:=1;
  198.                        Dec(BT);Dec(BB);
  199.                        MoveTo(BL+2,BT+BW div 2+1);
  200.                        LineTo(BL+2,BB-1);
  201.                        MoveTo(BL+3,BT+BW div 2);
  202.                        LineTo(BL+3,BB-2);
  203.                        MoveTo(BL+2,BB-1);
  204.                        LineTo(BR-2,BT+3);
  205.                        MoveTo(BL+3,BB-1);
  206.                        LineTo(BR-1,BT+3);
  207.                      end;
  208.             cbGrayed:begin
  209.                        if Down then
  210.                          begin
  211.                            Pen.Color:=clBtnFace;
  212.                            Brush.Color:=clBtnFace;
  213.                            Rectangle(BL+2,BT+2,BR-1,BB-1);
  214.                          end;
  215.                        Brush.Color:=clBtnShadow;
  216.                        Rectangle(BL+2,BT+2,BR-1,BB-1);
  217.                      end;
  218.          end;
  219.          Brush.Color:=clBtnFace;
  220.          Rect:=Bounds(TX-1,TY,TW+3,TH+1);
  221.          FrameRect(Rect);
  222.          if FFocused then
  223.            DrawFocusRect(Rect);
  224.        end;
  225. end;
  226.  
  227. function TBorCheck.GetCaption:TCaption;
  228.  
  229. begin
  230.      SetLength(Result,GetTextLen);
  231.      GetTextBuf(@Result[1],256);
  232. end;
  233.  
  234. procedure TBorCheck.SetCaption(const Value:TCaption);
  235.  
  236. var Buffer: array[0..255] of Char;
  237.  
  238. begin
  239.      if GetCaption <> Value then
  240.        SetTextBuf(StrPCopy(Buffer,Value));
  241.      Invalidate;
  242. end;
  243.  
  244. procedure TBorCheck.SetDown(Value:Boolean);
  245.  
  246. begin
  247.      if FDown<>Value then
  248.        begin
  249.          FDown:=Value;
  250.          Paint;
  251.        end;
  252. end;
  253.  
  254. procedure TBorCheck.SetState(Value:TCheckBoxState);
  255.  
  256. begin
  257.      if FState<>Value then
  258.        begin
  259.          FState:=Value;
  260.          Paint;
  261.          Click;
  262.        end;
  263. end;
  264.  
  265. function TBorCheck.GetChecked: Boolean;
  266.  
  267. begin
  268.      Result:=State=cbChecked;
  269. end;
  270.  
  271. procedure TBorCheck.SetChecked(Value:Boolean);
  272.  
  273. begin
  274.      if Value then State := cbChecked
  275.               else State := cbUnchecked;
  276. end;
  277.  
  278. procedure TBorCheck.SetCheckColor(Value:TColor);
  279.  
  280. begin
  281.      FCheckColor:=Value;
  282.      Paint;
  283. end;
  284.  
  285. procedure TBorCheck.DoEnter;
  286.  
  287. begin
  288.      inherited DoEnter;
  289.      FFocused:=True;
  290.      Paint;
  291. end;
  292.  
  293. procedure TBorCheck.DoExit;
  294.  
  295. begin
  296.      inherited DoExit;
  297.      FFocused:=False;
  298.      Paint;
  299. end;
  300.  
  301. procedure TBorCheck.MouseDown(Button: TMouseButton; Shift: TShiftState;
  302.                                   X, Y: Integer);
  303.  
  304. begin
  305.      SetFocus;
  306.      FFocused:=True;
  307.      inherited MouseDown(Button, Shift, X, Y);
  308.      MouseCapture:=True;
  309.      Down:=True;
  310. end;
  311.  
  312. procedure TBorCheck.MouseUp(Button: TMouseButton; Shift: TShiftState;
  313.                                   X, Y: Integer);
  314.  
  315. begin
  316.      MouseCapture:=False;
  317.      Down:=False;
  318.      if (X>=0) and (X<=Width) and (Y>=0) and (Y<=Height) then
  319.        Checked:=not Checked;
  320.      inherited MouseUp(Button, Shift, X, Y);
  321. end;
  322.  
  323. procedure TBorCheck.MouseMove(Shift: TShiftState;X, Y: Integer);
  324.  
  325. begin
  326.      if MouseCapture then
  327.        Down:=(X>=0) and (X<=Width) and (Y>=0) and (Y<=Height);
  328.      inherited MouseMove(Shift,X,Y);
  329. end;
  330.  
  331. procedure TBorCheck.KeyDown(var Key:Word;Shift:TShiftSTate);
  332.  
  333. begin
  334.      if Key=vk_Space then Down:=True;
  335.      inherited KeyDown(Key,Shift);
  336. end;
  337.  
  338. procedure TBorCheck.KeyUp(var Key:Word;Shift:TShiftSTate);
  339.  
  340. begin
  341.      if Key=vk_Space then
  342.        begin
  343.          Down:=False;
  344.          Checked:=not Checked;
  345.        end;
  346. end;
  347.  
  348. {-------------------------------------------------------------------}
  349. {                           Borland Radio Button                    }
  350. {-------------------------------------------------------------------}
  351.  
  352. constructor TBorRadio.Create(AOwner: TComponent);
  353.  
  354. begin
  355.   inherited Create(AOwner);
  356.   Width := 98;
  357.   Height := 20;
  358.   ParentColor:=False;
  359.   Color:=clBtnFace;
  360. end;
  361.  
  362. procedure TBorRadio.Paint;
  363.  
  364. var BL,BT,BR,BB,BM:Integer;
  365.     TX,TY,TW,TH:Integer;
  366.     CX,CY:Integer;
  367.     Rect:TRect;
  368.  
  369. begin
  370.      Canvas.Font:=Font;
  371.      with Canvas do
  372.        begin
  373.          BM:=BW div 2;
  374.          BT:=(Height div 2)-BM;
  375.          BB:=BT+BW;
  376.          BL:=1;
  377.          BR:=BW+1;
  378.          Brush.Color:=clBtnFace;
  379.          if Down then
  380.            begin
  381.              Pen.Color:=clBlack;
  382.              MoveTo(BL+BM,BT);
  383.              LineTo(BL,BT+BM);
  384.              LineTo(BL+BM,BB);
  385.              LineTo(BR,BT+BM);
  386.              LineTo(BL+BM,BT);
  387.              MoveTo(BL+BM,BT+1);
  388.              LineTo(BL+1,BT+BM);
  389.              LineTo(BL+BM,BB-1);
  390.              LineTo(BR-1,BT+BM);
  391.              LineTo(BL+BM,BT+1);
  392.            end
  393.          else
  394.            begin
  395.              Pen.Color:=clBtnFace;
  396.              Rectangle(BL,BT,BR,BB);
  397.              if Checked then Pen.Color:=clBtnShadow
  398.                         else Pen.Color:=clBtnHighLight;
  399.              MoveTo(BL+BM,BT);
  400.              LineTo(BL,BT+BM);
  401.              LineTo(BL+BM,BB);
  402.              if Checked then Pen.Color:=clBtnHighLight
  403.                         else Pen.Color:=clBtnShadow;
  404.              LineTo(BR,BT+BM);
  405.              LineTo(BL+BM,BT);
  406.            end;
  407.          if Checked then
  408.             begin
  409.               Pen.Color:=CheckColor;
  410.               CX:=BL+BM;CY:=BT+BM;
  411.               MoveTo(CX-1,CY-1);
  412.               LineTo(CX+2,CY-1);
  413.               MoveTo(CX-2,CY);
  414.               LineTo(CX+3,CY);
  415.               MoveTo(CX-1,CY+1);
  416.               LineTo(CX+2,CY+1);
  417.               MoveTo(CX,CY-2);
  418.               LineTo(CX,CY+3);
  419.             end;
  420.          TX:=BR+5;
  421.          TY:=(Height div 2)+(Font.Height div 2)-1;
  422.          TW:=TextWidth(Caption);
  423.          TH:=TextHeight(Caption);
  424.          TextOut(TX,TY,Caption);
  425.          Brush.Color:=clBtnFace;
  426.          Rect:=Bounds(TX-1,TY,TW+3,TH+1);
  427.          FrameRect(Rect);
  428.          if FFocused then
  429.            DrawFocusRect(Rect);
  430.        end;
  431. end;
  432.  
  433. function TBorRadio.GetCaption:TCaption;
  434.  
  435. begin
  436.      SetLength(Result,GetTextLen);
  437.      GetTextBuf(@Result[1],256);
  438. end;
  439.  
  440. procedure TBorRadio.SetCaption(const Value:TCaption);
  441.  
  442. var Buffer: array[0..255] of Char;
  443.  
  444. begin
  445.      if GetCaption <> Value then
  446.        SetTextBuf(StrPCopy(Buffer,Value));
  447.      Invalidate;
  448. end;
  449.  
  450. procedure TBorRadio.SetDown(Value:Boolean);
  451.  
  452. begin
  453.      if FDown<>Value then
  454.        begin
  455.          FDown:=Value;
  456.          Paint;
  457.        end;
  458. end;
  459.  
  460. procedure TBorRadio.TurnSiblingsOff;
  461.  
  462. var i:Integer;
  463.     Sibling: TBorRadio;
  464.  
  465. begin
  466.      if Parent <> nil then
  467.        for i:=0 to Parent.ControlCount-1 do
  468.          if Parent.Controls[i] is TBorRadio then
  469.            begin
  470.              Sibling:=TBorRadio(Parent.Controls[i]);
  471.              if (Sibling<>Self) and
  472.                 (Sibling.GroupIndex=GroupIndex) then
  473.                   Sibling.SetChecked(False);
  474.            end;
  475. end;
  476.  
  477. procedure TBorRadio.SetChecked(Value: Boolean);
  478.  
  479. begin
  480.      if FChecked <> Value then
  481.        begin
  482.          TabStop:=Value;
  483.          FChecked:=Value;
  484.          if Value then
  485.            begin
  486.              TurnSiblingsOff;
  487.              Click;
  488.            end;
  489.          Paint;
  490.        end;
  491. end;
  492.  
  493. procedure TBorRadio.SetCheckColor(Value:TColor);
  494.  
  495. begin
  496.      FCheckColor:=Value;
  497.      Paint;
  498. end;
  499.  
  500. procedure TBorRadio.DoEnter;
  501.  
  502. begin
  503.      inherited DoEnter;
  504.      FFocused:=True;
  505.      Checked:=True;
  506.      Paint;
  507. end;
  508.  
  509. procedure TBorRadio.DoExit;
  510.  
  511. begin
  512.      inherited DoExit;
  513.      FFocused:=False;
  514.      Paint;
  515. end;
  516.  
  517. procedure TBorRadio.MouseDown(Button: TMouseButton; Shift: TShiftState;
  518.                                   X, Y: Integer);
  519.  
  520. begin
  521.      SetFocus;
  522.      FFocused:=True;
  523.      inherited MouseDown(Button, Shift, X, Y);
  524.      MouseCapture:=True;
  525.      Down:=True;
  526. end;
  527.  
  528. procedure TBorRadio.MouseUp(Button: TMouseButton; Shift: TShiftState;
  529.                                   X, Y: Integer);
  530.  
  531. begin
  532.      MouseCapture:=False;
  533.      Down:=False;
  534.      if (X>=0) and (X<=Width) and (Y>=0) and (Y<=Height)
  535.        and not Checked then Checked:=True;
  536.      inherited MouseUp(Button, Shift, X, Y);
  537. end;
  538.  
  539. procedure TBorRadio.MouseMove(Shift: TShiftState;X, Y: Integer);
  540.  
  541. begin
  542.      if MouseCapture then
  543.        Down:=(X>=0) and (X<=Width) and (Y>=0) and (Y<=Height);
  544.      inherited MouseMove(Shift,X,Y);
  545. end;
  546.  
  547. procedure TBorRadio.KeyDown(var Key:Word;Shift:TShiftSTate);
  548.  
  549. begin
  550.      if Key=vk_Space then Down:=True;
  551.      inherited KeyDown(Key,Shift);
  552. end;
  553.  
  554. procedure TBorRadio.KeyUp(var Key:Word;Shift:TShiftSTate);
  555.  
  556. begin
  557.      if Key=vk_Space then
  558.        begin
  559.          Down:=False;
  560.          if not Checked then Checked:=True;
  561.        end;
  562. end;
  563.  
  564. procedure Register;
  565.  
  566. begin
  567.      RegisterComponents('Samples',[TBorCheck,TBorRadio]);
  568. end;
  569.  
  570. end.
  571.